package com.x.component.widget;

import java.util.List;

import android.content.ComponentName;
import android.graphics.Bitmap;
import android.media.MediaMetadata;
import android.media.session.MediaController;
import android.media.session.MediaSessionManager;
import android.service.notification.NotificationListenerService;
import android.media.session.PlaybackState;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class MediaWidgetService extends NotificationListenerService {

	private static final String TAG = "MediaWidgetHost";

	private MediaSessionManager mediaSessionManager;

	private List<MediaController> mActiveSessions;

	private MediaController.Callback mSessionCallback;

	@Override
	public void onCreate() {
		super.onCreate();

	}

	@Override
	public void onListenerConnected() {
		// super.onListenerConnected();
		Log.d("MediaWidgetService", "onListenerConnected");
		initMediaSessionManager();
	}

	@Override
	public void onListenerDisconnected() {
		super.onListenerDisconnected();
		Log.d("MediaWidgetService", "onListenerDisconnected");
	}

	private void initMediaSessionManager() {
		mediaSessionManager = (MediaSessionManager) getSystemService(MEDIA_SESSION_SERVICE);
		ComponentName localComponentName = new ComponentName(this, MediaWidgetService.class);
		mediaSessionManager
				.addOnActiveSessionsChangedListener(new MediaSessionManager.OnActiveSessionsChangedListener() {
					@Override
					public void onActiveSessionsChanged(final List<MediaController> controllers) {
						try {
							synchronized (this) {
								mActiveSessions = controllers;
								registerSessionCallbacks();
							}
						} catch (Exception e) {
							Log.d(TAG, "onActiveSessionsChanged has a exception: " + e.toString());
						}
					}
				}, localComponentName);
		synchronized (this) {
			mActiveSessions = mediaSessionManager.getActiveSessions(localComponentName);
			registerSessionCallbacks();
		}

	}

	private void registerSessionCallbacks() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			for (MediaController controller : mActiveSessions) {
				if (mSessionCallback == null) {
					mSessionCallback = new MediaController.Callback() {
						@Override
						public void onMetadataChanged(MediaMetadata metadata) {
							if (metadata != null) {
								Bitmap bitmap = metadata.getBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART);
								String title = metadata.getString(MediaMetadata.METADATA_KEY_TITLE);
								String artist = metadata.getString(MediaMetadata.METADATA_KEY_ARTIST);
								String albumArtist = metadata.getString(MediaMetadata.METADATA_KEY_ALBUM_ARTIST);
								String album = metadata.getString(MediaMetadata.METADATA_KEY_ALBUM);

								Log.d(TAG, "onMetadataChanged title: " + title);
								Log.d(TAG, "onMetadataChanged artist: " + artist);
								Log.d(TAG, "onMetadataChanged album: " + album);
								Log.d(TAG, "onMetadataChanged albumArtist: " + albumArtist);
								Handler foregroundHandler = MediaWidgetHost.getInstance().getForegroundHandler();
								Message msg = Message.obtain(foregroundHandler, 101);
								msg.obj = metadata;
								msg.sendToTarget();
							}
						}

						@Override
						public void onPlaybackStateChanged(PlaybackState state) {
							if (state != null) {
								boolean isPlaying = state.getState() == PlaybackState.STATE_PLAYING;
								Log.d(TAG, "onPlaybackStateChanged isPlaying: " + isPlaying);
								Handler foregroundHandler = MediaWidgetHost.getInstance().getForegroundHandler();
								Message msg = Message.obtain(foregroundHandler, 100);
								msg.obj = state;
								msg.sendToTarget();
							}
						}

					};
				}
				controller.registerCallback(mSessionCallback);
			}
		}
	}

}
